package com.xujiang.imitationcompanypj.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.etekcity.vesync.common.adapter.IBaseAdapter;
import com.etekcity.vesync.common.adapter.core.HandleView;
import com.etekcity.vesync.common.adapter.core.RecyclerViewHelper;
import com.etekcity.vesync.common.adapter.view.FixTouchSlopSwipeRefreshLayout;
import com.etekcity.vesync.common.util.UIUtils;
import com.wang.avi.AVLoadingIndicatorView;
import com.xujiang.imitationcompanypj.R;
import com.xujiang.imitationcompanypj.adapter.UserCenterActionListAdapter;
import com.xujiang.imitationcompanypj.data.ui.base.BaseLazyFragment;
import com.xujiang.imitationcompanypj.model.UserActionItemEntity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UserCenterFragment extends BaseLazyFragment {

    List<UserActionItemEntity> data = new ArrayList<>();
    @BindView(R.id.ek_ca_list_recycler_view)
    RecyclerView ekCaListRecyclerView;
    @BindView(R.id.ek_ca_list_swipe_container)
    FixTouchSlopSwipeRefreshLayout ekCaListSwipeContainer;
    @BindView(R.id.ek_cp_progress)
    AVLoadingIndicatorView ekCpProgress;
    @BindView(R.id.ek_cp_progress_layout)
    FrameLayout ekCpProgressLayout;


    public static Fragment newInstance() {
        return new UserCenterFragment();
    }

    public UserCenterFragment() {
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //定义列表的图标和文字类容，每行数据一一对应
        int[] userActionIconAry = new int[]{
                R.mipmap.ic_user_device_manager,
                R.mipmap.ic_user_group,
                R.mipmap.ic_user_sale_service,
                R.mipmap.ic_user_settings
        };

        String[] userActionNameAry = new String[]{
                getContext().getString(R.string.uc_menu_device_manager),
                getContext().getString(R.string.uc_menu_home_manager),
                getContext().getString(R.string.uc_menu_sale_service),
                getContext().getString(R.string.uc_menu_settings)
        };
        data = new ArrayList<>();
        for (int i = 0; i < userActionIconAry.length; i++) {
            UserActionItemEntity ua = new UserActionItemEntity();
            ua.setUserActionIconDrawableId(userActionIconAry[i]);
            ua.setUserActionName(userActionNameAry[i]);
            data.add(ua);
        }
    }




    class HeaderViewHolder extends HandleView.EmptyHandleView {

        @BindView(R.id.user_center_portrait)
        ImageView userCenterPortrait;
        @BindView(R.id.btn_user_center_to_login)
        Button btnUserCenterToLogin;
        @BindView(R.id.btn_user_center_to_reg)
        Button btnUserCenterToReg;
        @BindView(R.id.user_content_not_sign)
        LinearLayout userContentNotSign;
        @BindView(R.id.user_center_username)
        TextView userCenterUsername;
        @BindView(R.id.user_center_signature)
        TextView userCenterSignature;
        @BindView(R.id.user_content_sign)
        LinearLayout userContentSign;

        @Override
        public void handle(View view) {
            ButterKnife.bind(this, view);
        }

        @OnClick({R.id.user_center_portrait, R.id.btn_user_center_to_login, R.id.btn_user_center_to_reg, R.id.user_center_username, R.id.user_center_signature})
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.user_center_portrait:
//                    if (UserLogin.get().isLogin()) {
//                        UIUtils.startActivity(getActivity(), UserDetailActivity.class);
//                    } else {
//                        LoginActivity.login(getActivity());
//                    }
                    break;
                case R.id.btn_user_center_to_login:
                    //LoginActivity.login(getActivity());
                    break;
                case R.id.btn_user_center_to_reg:
                    //UIUtils.startActivity(getActivity(), RegisterActivity.class);
                    break;
                case R.id.user_center_username:
                    UIUtils.startActivity(getActivity(),null);
                    break;
                case R.id.user_center_signature:
                    UIUtils.startActivity(getActivity(),null);
                    break;
            }
        }

        public void updateUserInfo(){

        }


    }

    HeaderViewHolder mHeaderViewHolder;

    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        super.onCreateViewLazy(savedInstanceState);
        setContentView(R.layout.fragment_user_center);
        ButterKnife.bind(this, getContentView());
        mHeaderViewHolder = new HeaderViewHolder();
        RecyclerViewHelper helper = new RecyclerViewHelper.Builder(getActivity(), getContentView())
                .addHeaderView(R.layout.view_user_center_header, mHeaderViewHolder).build();
        UserCenterActionListAdapter userCenterActionListAdapter = new UserCenterActionListAdapter(getActivity(),data);
        userCenterActionListAdapter.setOnItemClickListener(new IBaseAdapter.OnItemClickListener<UserActionItemEntity>() {
            @Override
            public void onItemClick(UserActionItemEntity item, View view, int position) {

            }
        });
        helper.setAdapter(userCenterActionListAdapter);
        mHeaderViewHolder.updateUserInfo();

    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateUIEvent(UpdateUIEvent e) {
        mHeaderViewHolder.updateUserInfo();
    }

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN) public void updateUIStickyEvent(
            UpdateUIStickyEvent e) {
        mHeaderViewHolder.updateUserInfo();
        EventBus.getDefault().removeStickyEvent(e);
    }


    //    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container,
//                             Bundle savedInstanceState) {
//        // Inflate the layout for this fragment
//        return inflater.inflate(R.layout.fragment_user_center, container, false);
//    }

}
